<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 软文列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <!-- <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>-->
                软文标题:
                <el-input
                    v-model="query.articleTitle"
                    placeholder="软文标题"
                    class="handle-input mr10"
                    style="width: 200px"
                    @change="handleSearch"
                ></el-input>
                <!-- <el-input v-model="query.author" placeholder="作者" class="handle-input mr10" style="width:100px"></el-input> -->
                作者:
                <el-input
                    v-model="query.author"
                    placeholder="作者"
                    class="handle-input mr10"
                    style="width: 120px"
                    @change="handleSearch"
                ></el-input>
                所属栏目:
                <el-select
                    v-model="query.programaType"
                    placeholder="所属栏目"
                    clearable
                    @clear="clear"
                    @change="changeSele"
                    class="handle-select mr10"
                >
                    <el-option key="0" label="在线会展" value="0"></el-option>
                    <el-option key="2" label="企投家" value="2"></el-option>
                </el-select>
                是否推荐:
                <el-select v-model="query.isRecommend" placeholder="是否推荐" clearable class="handle-select mr10">
                    <el-option key="0" label="是" value="0"></el-option>
                    <el-option key="1" label="否" value="1"></el-option>
                </el-select>
                <!-- <el-select v-model="query.isRecommend" placeholder="是否推荐页" clearable @clear="clear2" @change='changeSele2'  class="handle-select mr10">
                    <el-option key="0" label="不推荐" value="0"></el-option>
                    <el-option key="1" label="推荐" value="1"></el-option>
                </el-select>
                 <el-select v-model="query.articleType" placeholder="软文类型" clearable @clear="clear3" @change='changeSele3'  class="handle-select mr10">
                    <el-option key="0" label="软文详情" value="0"></el-option>
                    <el-option key="1" label="主题展" value="1"></el-option>
                    <el-option key="2" label="公司" value="2"></el-option>
                    <el-option key="3" label="产品" value="3"></el-option>
                    <el-option key="4" label="产品详情" value="4"></el-option>
                </el-select>
                <el-input v-model="query.name" placeholder="用户名" class="handle-input mr10"></el-input>-->
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" style="display: block; margin: 10px 0px" @click="toBaseTableAdd">+ 新增软文</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
                :header-cell-style="{ 'text-align': 'center' }"
            >
                <el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <!-- <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column> -->
                <el-table-column prop="articleTitle" label="标题"></el-table-column>
                <el-table-column prop="author" label="作者" width="100" align="center"></el-table-column>
                <!-- <el-table-column label="账户余额">
                    <template slot-scope="scope">￥{{scope.row.money}}</template>
                </el-table-column>-->
                <el-table-column label="作者头像" width="100" align="center">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-thumb"
                            :src="scope.row.authorHeadImg"
                            :preview-src-list="[scope.row.authorHeadImg]"
                        ></el-image>
                        <!-- v-if="scope.row.authorHeadImg"
                        <span>111</span> -->
                    </template>
                </el-table-column>
                <!-- <el-table-column label="作者类型" width="150" align="center"></el-table-column> -->
                <el-table-column prop="programaType" label="所属栏目" width="150" align="center">
                    <template slot-scope="scope">
                        <div>
                            <span v-if="scope.row.programaType == 0">在线会展</span>
                            <span v-if="scope.row.programaType == 2">企投家</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="createTime" label="创建时间" width="180" align="center"></el-table-column>
                <el-table-column label="是否推荐" align="center">
                    <template slot-scope="scope">
                        <div>
                            <span v-if="scope.row.isRecommend == 0">是</span>
                            <span v-if="scope.row.isRecommend == 1">否</span>
                        </div>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.state==='成功'?'success':(scope.row.state==='失败'?'danger':'')"
                        >{{scope.row.state}}</el-tag>
                    </template>
                </el-table-column>-->

                <!-- <el-table-column prop="date" label="注册时间"></el-table-column> -->
                <el-table-column label="操作" width="300" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" @click="toDetail(scope.$index, scope.row)">查看</el-button>
                        <el-button type="success" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="pageNum"
                    :page-size="pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 查看的弹出框 -->
        <el-dialog title="产品详细资料" :visible.sync="baseVisible" width="50%">
            <el-form ref="form" :model="baseTableData" label-width="120px">
                <el-form-item label="作者名称">
                    <el-input v-model="baseTableData.author" style="width: 250px" disabled></el-input>
                </el-form-item>
                <el-form-item label="作者LOGO">
                    <el-image
                        style="width: 80px"
                        class="check_image"
                        :src="baseTableData.authorHeadImg"
                        :preview-src-list="[baseTableData.authorHeadImg]"
                    ></el-image>
                </el-form-item>
                <el-form-item label="作品标题">
                    <el-input type="textarea" rows="4" v-model="baseTableData.articleTitle" style="width: 350px" disabled></el-input>
                </el-form-item>
                <el-form-item label="作品图片">
                    <el-image
                        style="width: 80px"
                        class="check_image"
                        :src="baseTableData.titleImg"
                        :preview-src-list="[baseTableData.titleImg]"
                    ></el-image>
                </el-form-item>
                <el-form-item label="作品简介">
                    <el-input type="textarea" rows="6" v-model="baseTableData.articleIntroduce" style="width: 450px" disabled></el-input>
                </el-form-item>
                <el-form-item label="创建时间">
                    <el-input v-model="baseTableData.createTime" style="width: 250px" disabled></el-input>
                </el-form-item>
                <el-form-item label="作品富文本">
                    <div v-html="baseTableData.richText"></div>
                </el-form-item>
                <el-form-item label="是否推荐">
                    <el-radio v-model="baseTableData.isRecommend" :label="0" disabled>是</el-radio>
                    <el-radio v-model="baseTableData.isRecommend" :label="1" disabled>否</el-radio>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="baseVisible = false">取 消</el-button>
                <!-- <el-button type="primary" @click="saveEdit(form)">确 定</el-button> -->
            </span>
        </el-dialog>
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import axios from 'axios';
//import { fetchData } from '../../api/index';
//import {getArticleList} from '../../api/articleList'
import { rgetArticleList } from '../../api/article';
import { async } from 'q';
import { log } from 'util';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                address: '',
                name: '',
                pageIndex: 1,

                articleTitle: '',
                author: '',
                programaType: '',
                isRecommend: '',
                articleType: ''
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            baseVisible: false,
            baseTableData: {
                isRecommend: ''
            },
            pageTotal: 0,
            pageNum: 1, //起始页
            pageSize: 10,
            form: {},
            idx: -1,
            id: -1
        };
    },
    mounted() {
        this.getData();
        //this._getArticleList()
    },
    created() {},
    methods: {
        // 测试
        toBaseTableAdd() {
            this.$router.push('/BaseTableAdd');
        },
        // isProgramaType(type) {
        //     if (type == 0) {
        //         return '在线会展';
        //     }
        //     if (type == 1) {
        //         return '时代人物';
        //     }
        //     if (type == 2) {
        //         return '企投家';
        //     }
        // },
        clear() {
            this.query.programaType = '';
        },
        changeSele(val) {
            this.query.programaType = val;
        },
        clear2() {
            this.query.isRecommend = '';
        },
        changeSele2(val) {
            this.query.isRecommend = val;
        },
        clear3() {
            this.query.articleType = '';
        },
        changeSele3(val) {
            this.query.articleType = val;
            //console.log(val);
        },
        toDetail(index, row) {
            this.baseVisible = true;
            console.log(row);
            axios.get(this.$myUrl.BASEURL + 'article/getArticle?articleId=' + row.id).then((res) => {
                console.log('查看软文数据', res);
                this.baseTableData = res.data.data;
                this.baseTableData.authorHeadImg = this.$myUrl.IMGURL + this.baseTableData.authorHeadImg;
                this.baseTableData.titleImg = this.$myUrl.IMGURL + this.baseTableData.titleImg;
                // this.baseTableData.isRecommend = res.data.data.isRecommend
            });
        },
        // 获取 easy-mock 的模拟数据
        getData() {
    
            axios.get(this.$myUrl.BASEURL + 'article/getArticleList?pageNum=' + this.pageNum + '&pageSize=' + this.pageSize).then((res) => {
                console.log('软文列表', res);
                this.tableData = res.data.data;
                this.pageTotal = res.data.totalNumber;
                //console.log(this.pageTotal)
                this.tableData.forEach((item, index) => {
                    ////console.log(item)
                    if (item.authorHeadImg == '') {
                        item.authorHeadImg = '';
                    } else {
                        item.authorHeadImg = this.$myUrl.IMGURL + item.authorHeadImg;
                    }
                    item.programaType = this.isProgramaType(item.programaType);
                });
            });
        },
        // 触发搜索按钮
        handleSearch() {

            //this.$set(this.query, 'pageIndex', 1);
            //this._getArticleList()
            // this.$router.push({
            //     path:'/content-detail'
            // })
            this.tableData = [];
            this.searchData = [];
            axios
                .get(
                    this.$myUrl.BASEURL +
                        'article/getArticleList?articleTitle=' +
                        this.query.articleTitle +
                        '&author=' +
                        this.query.author +
                        '&programaType=' +
                        this.query.programaType +
                        '&isRecommend=' +
                        this.query.isRecommend +
                        '&pageNum=' +
                        this.pageNum +
                        '&pageSize=' +
                        this.pageSize
                )
                .then((res) => {
                    console.log('软文搜索结果', res);
                    this.tableData = res.data.data;
                    this.pageTotal = res.data.totalNumber;
                    //console.log(this.pageTotal)
                    this.tableData.forEach((item, index) => {
                        ////console.log(item)
                        if (item.authorHeadImg == '') {
                            item.authorHeadImg = '';
                        } else {
                            item.authorHeadImg = this.$myUrl.IMGURL + item.authorHeadImg;
                        }
                        item.programaType = this.isProgramaType(item.programaType);
                    });
                });
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            console.log(row);
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    axios.delete(this.$myUrl.BASEURL + 'article/deleteArticle?articleId=' + row.id).then((res) => {
                        console.log(res);
                        if (res.data.code == 200) {
                            this.$message.success('删除成功');
                            // this.tableData.splice(index, 1);
                            this.getData();
                        }
                    });
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            console.log('row', row.id);
            this.idx = index;
            this.form = row;
            // this.editVisible = true;
            this.$router.push('/BaseTableAdd?articleId=' + row.id);
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            // this.$set(this.query, 'pageNum', val);
            //console.log("page====>>" + val)
            this.pageNum = val;
            this.handleSearch();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 80px;
    height: 80px;
    border-radius: 50%;
}
</style>
